package com.shushan.home.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.home.bean.RecordDetailBean;
import com.shushan.home.bean.TotalPlayBean;
import com.shushan.shushanbao.R;
import com.shushan.ui.OffsetRecyclerDivider;
import com.shushan.util.DateUtils;
import com.shushan.util.DensityUtil;
import com.shushan.util.ToolUtils;

import java.util.List;

/**
 * @ClassName: StudyDetailAdapter
 * @Desciption: //学习记录详情中APP使用时间适配器
 * @author: zhangshihao
 * @date: 2018-09-19
 */
public class StudyDetailAdapter extends RecyclerView.Adapter<StudyDetailAdapter.ViewHolder> {

    private static final String TAG = StudyDetailAdapter.class.getSimpleName();

    private Context mContext;
    private OnTitleBarClickListener onTitleBarClickListener;
    private List<RecordDetailBean> recordDetailBeans;
    private int type;

    public StudyDetailAdapter(Context context, List<RecordDetailBean> recordDetailBeans, int type) {
        mContext = context;
        this.recordDetailBeans = recordDetailBeans;
        this.type = type;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_study_statistics, null));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final RecordDetailBean detailBean = recordDetailBeans.get(position);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        holder.recyclerView.addItemDecoration(new OffsetRecyclerDivider(DensityUtil.dip2px(mContext, 0.5)));
        holder.recyclerView.setAdapter(new AppUseTimeAdapter(detailBean.getPlayListBeans(), detailBean.today_play_time));
        holder.llTitleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onTitleBarClickListener != null) {
                    onTitleBarClickListener.onTitleBarClick(position, detailBean.id, detailBean.practice_type);
                }
            }
        });
        if (type < 0) {
            if(detailBean.practice_type == Constants.TYPE_ORAL_CALCULATE_EXERCISE){
                holder.tvPracticeType.setText(ToolUtils.getExerciseTypeNameByTypeId(detailBean.practice_type) + "做对: ");
                holder.tvScore.setText(detailBean.right_count + "题");
            }else {
                holder.tvPracticeType.setText(ToolUtils.getExerciseTypeNameByTypeId(detailBean.practice_type) + "得分: ");
                holder.tvScore.setText(detailBean.score + "分");
            }

            holder.tvTime.setText(DateUtils.getDate(detailBean.create_time, "HH:mm"));
        } else {
            holder.tvPracticeType.setText(DateUtils.getDate(detailBean.create_time, "yyyy-MM-dd HH:mm"));
            holder.tvScore.setVisibility(View.GONE);
            if(detailBean.practice_type == Constants.TYPE_ORAL_CALCULATE_EXERCISE){
                holder.tvTime.setText(detailBean.right_count + "题");
            }else {
                holder.tvTime.setText(detailBean.score + "分");
            }

        }
        holder.tvPlayTime.setText("获得" + detailBean.today_play_time + "分钟");
    }

    @Override
    public int getItemCount() {
        return recordDetailBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RecyclerView recyclerView;
        private LinearLayout llTitleBar;
        private TextView tvPracticeType;
        private TextView tvScore;
        private TextView tvTime;
        private TextView tvPlayTime;

        public ViewHolder(View itemView) {
            super(itemView);
            recyclerView = itemView.findViewById(R.id.rv_app_use_time);
            llTitleBar = itemView.findViewById(R.id.ll_title_bar);
            tvPracticeType = itemView.findViewById(R.id.tv_practice_type);
            tvScore = itemView.findViewById(R.id.tv_score);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvPlayTime = itemView.findViewById(R.id.tv_play_time);
        }
    }

    public class AppUseTimeAdapter extends RecyclerView.Adapter<AppUseTimeAdapter.ItemViewHolder> {

        private List<RecordDetailBean.PlayListBean> playListBeans;
        private int totalPlayTime;//分钟

        public AppUseTimeAdapter(List<RecordDetailBean.PlayListBean> playListBeans, int totalPlayTime) {
            this.playListBeans = playListBeans;
            this.totalPlayTime = totalPlayTime;
        }

        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ItemViewHolder holder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_study_detail_app_use_time, null));
            return holder;
        }

        @Override
        public void onBindViewHolder(ItemViewHolder holder, int position) {
            if (playListBeans == null) {
                return;
            }

            RecordDetailBean.PlayListBean listBean = playListBeans.get(position);
            holder.tvAppName.setText(ToolUtils.getPreThreeStr(listBean.app_name));
            holder.progressBar.setProgress((int) ((float) Integer.parseInt(listBean.duration) / (totalPlayTime * 60) * 100));
            holder.tvUseTime.setText((int) ((float) Integer.parseInt(listBean.duration) / 60) + "分钟");
            if(position == (playListBeans.size()-1)){
                holder.viewDivider.setVisibility(View.GONE);
            }
        }

        @Override
        public int getItemCount() {
            return playListBeans.size();
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {
            private TextView tvAppName;
            private ProgressBar progressBar;
            private TextView tvUseTime;
            private View viewDivider;
            public ItemViewHolder(View itemView) {
                super(itemView);
                tvAppName = itemView.findViewById(R.id.tv_app_name);
                progressBar = itemView.findViewById(R.id.progressBar);
                tvUseTime = itemView.findViewById(R.id.tv_use_time);
                viewDivider = itemView.findViewById(R.id.v_divider);
            }
        }

    }

    public void setOnTitleBarClickListener(OnTitleBarClickListener onTitleBarClickListener) {
        this.onTitleBarClickListener = onTitleBarClickListener;
    }

    public interface OnTitleBarClickListener {
        void onTitleBarClick(int position, int paperId, int practiceType);
    }

}
